var selectedDatapermi = [];
/**
 * 切换角色触发数据字典事件
 */
function changeRoleDatapermi() {
    $("#m_tabs_2_3 .select-group").empty();
    getSelectedDatapermi();
    reloadDatapermi();
}



/**
 * 重新渲染数据权限选择
 */
function reloadDatapermi() {
    $("#datapermi-div").empty();
    laytpl($("#datapermi-select-template").html()).render({list: DatapermiTypes}, function (html) {
        $("#datapermi-div").append(html);
    });

    for (var i = 0; i < DatapermiTypes.length; i++) {
        for (var j = 0; j < selectedDatapermi.length; j++) {
            for (var m = 0; m < Datapermis[DatapermiTypes[i].id].length; m++) {
                if (selectedDatapermi[j] == Datapermis[DatapermiTypes[i].id][m].id) {
                    Datapermis[DatapermiTypes[i].id][m]['selected'] = 'selected';
                }
            }
            $("#" + DatapermiTypes[i].code + "-select").val(selectedDatapermi[j]).trigger("change");
        }
        laytpl($("#datapermiOption").html()).render({list: Datapermis[DatapermiTypes[i].id]}, function (html) {
            $("#" + DatapermiTypes[i].code + "-select").append(html);
            $("#" + DatapermiTypes[i].code + "-select").select2({
                placeholder:"请选择" + DatapermiTypes[i].name + "类型数据权限",
                allowClear: true,
                width: '100%'
            });
        });
        $(".select2-search__field").css("width", "100%");
    }

}

/**
 * 保存角色菜单
 * @param roleid
 */
function saveDatapermi() {
    var roleid = roleTree.jstree("get_selected")[0];
    var datas = [];
    for (var i = 0; i < DatapermiTypes.length; i++) {
        var vals = $("#" + DatapermiTypes[i].code + "-select").val();
        var typeid = $("#" + DatapermiTypes[i].code + "-select").data("typeid");
        for (var j = 0; j < vals.length; j++) {
            datas.push({
                typeid: typeid,
                dataid: vals[j],
                roleid: roleid
            })
        }
    }
    $.hcPostAjax({
        msgPrefix: '保存角色权限数据失败：',
        url: basePath + '/datapermi/' + roleid,
        contentType: "application/json",
        data: JSON.stringify(datas),
        success: function () {
            layer.msg('保存成功！', {icon: 1,time:1500}, function () {
                getSelectedDatapermi();
            });
        }
    });
}

